﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Web;
using Ext.Net;

namespace ExaminationSys.Helper
{
    public class ExcelHelper
    {
        /// <summary>
        /// 获取excel SheetNames
        /// </summary>
        /// <param name="filepath">路径</param>
        /// <returns>SheetNames List</returns>
        public static ArrayList GetExcelSheetName(string filepath)
        {
            ArrayList al = new ArrayList();
            if (filepath != null && filepath.Length > 0)
            {
                string strFileName = Path.GetExtension(filepath).ToLower();
                string strConn;
                if (strFileName == ".xls")
                {
                    strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath +
                              ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1;\";";
                }
                else
                {
                    strConn =
                        string.Format(
                            "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'",
                            filepath);
                }
                using (OleDbConnection conn = new OleDbConnection(strConn))
                {
                    conn.Open();
                    DataTable sheetNames = conn.GetOleDbSchemaTable
                        (OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });

                    foreach (DataRow dr in sheetNames.Rows)
                    {
                        al.Add(dr[2]);
                    }
                }
            }
            return al;
        }

        public static void InitCbSheetData(string filePath, Store store)
        {
            if (!File.Exists(filePath)) return;
            var sheetNames = GetExcelSheetName(filePath);
            if (sheetNames.Count > 0)
            {
                var list = new List<object>(sheetNames.Count);
                foreach (string sheetName in sheetNames)
                {
                    list.Add(new
                    {
                        SheetName = sheetName,
                        SheetValue = sheetName
                    });
                }
                store.DataSource = list;
                store.DataBind();
            }
        }

        public static string FileUpload(string filename)
        {
            string temp = string.Empty;
            if (filename != null && filename.Length > 0)
            {
                string FilePath = "/UpLoad/" + filename;
                System.Web.HttpFileCollection _files = System.Web.HttpContext.Current.Request.Files;
                System.Web.HttpPostedFile _postedFile = _files[0];

                if (!string.IsNullOrEmpty(FilePath))
                {
                    if (System.IO.File.Exists(System.Web.HttpContext.Current.Request.MapPath(FilePath)))
                    {
                        System.IO.File.Delete(System.Web.HttpContext.Current.Request.MapPath(FilePath));
                    }
                    _postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath(FilePath));
                }
            }
            return temp;
        }
    }
}